//
//  HashUtility.m
//  HSBCHybrid
//
//  Created by HSBC on 11-03-21.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//


#include <CommonCrypto/CommonDigest.h>
#import "HashUtility.h"
#import <Security/Security.h>
#import "NSData+Base64.h"

//#define RSA_KEY_BASE64 @"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5AneKn94ybxDe1mGos8GpORlW9Rg2smY7NcbQC5Jb2miJh3zc+xaGp0GOGAI/WJGatElbRdbClB36fYOaMLvtU7jI1A7QNxnrciXx4MAZCgAVbBiDau0s/Ri6PymY6Jt6MOgrW4VGFfa8I80fW4tHxBIIyvhZ299adF2jeeYLUoadNNycfJks3sy4/ve9QgkvJfa2DZ6Z/jf5Bek+YbxQLxPJcFkNmMPIP5T+nWeBOEdQzC3rLxzhKXoiPpzJL/i+Z9GXjmMZxathIJYQVxxQsGJaS5N4MChyMsrGBNDGcxZTgocclcnm/JLeuzg0TU/ywCAdpyw/O+6OiF0uJNFQQIDAQAB"

//static SecKeyRef _public_key=nil;

@implementation HashUtility


+ (NSString *)createSHA512Hash:(NSString *) source 
{
	
	const char *s = [source cStringUsingEncoding:NSASCIIStringEncoding];  
	
	NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
	NSLog(@"inside createSHAHash: key data description %@" , [keyData description]);	 
	uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
	
	CC_SHA512(keyData.bytes, keyData.length, digest);
	
	NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
	NSLog(@"inside createSHAHash: out data description %@" , [out description]);	 
	
	//NSString *hashedUDID = [[[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding] autorelease];
	//NSLog(@"inside createSHAHash: out to string = %@" , hashedUDID);	
	
	NSString *hashedUDIDtmp1 = [[out description] stringByReplacingOccurrencesOfString:@" " withString:@""];
	NSString *hashedUDIDtmp2 = [hashedUDIDtmp1 stringByReplacingOccurrencesOfString:@"<" withString:@""];
	NSString *hashedUDIDfull = [hashedUDIDtmp2 stringByReplacingOccurrencesOfString:@">" withString:@""];
		
	return hashedUDIDfull;
}

+ (NSString *)getSHA512Hash:(NSData *)sourceData
{
    uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
    
    CC_SHA512(sourceData.bytes, sourceData.length, digest);
    
    NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
	//NSLog(@"inside getSHA512Hash: out data description %@" , [out description]);
    
	//NSString *hashedUDID = [[[NSString alloc] initWithData:out encoding:NSUTF8StringEncoding] autorelease];
	//NSLog(@"inside getSHA512Hash: out to string = %@" , hashedUDID);	
	
	NSString *hashedUDIDtmp1 = [[out description] stringByReplacingOccurrencesOfString:@" " withString:@""];
	NSString *hashedUDIDtmp2 = [hashedUDIDtmp1 stringByReplacingOccurrencesOfString:@"<" withString:@""];
	NSString *hashedUDIDfull = [hashedUDIDtmp2 stringByReplacingOccurrencesOfString:@">" withString:@""];
    
    //NSLog(@"inside getSHA512Hash: out to string = %@" , hashedUDIDfull);
	
    return hashedUDIDfull;
}

+ (BOOL) isCheckSumVerify
{
    return YES;
}

+ (BOOL) isCheckSumDecrypt
{
    NSNumber *checkSumDecryptFlag = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"HSBC_CHECKSUM_DECRYPT_FLAG"];
    
    BOOL flag = NO;
    
    if (checkSumDecryptFlag !=nil && ([checkSumDecryptFlag boolValue] == YES))
    {
        flag = YES;
    }
    
    return flag;
}

+ (NSString *)getDecryptString:(NSString *)cryptString
{
    NSString *planText = nil;
    
    planText = cryptString;
        
    return planText;
}

/*
+ (SecKeyRef) getPublicKey
{ 
    // 从公钥证书文件中获取到公钥的SecKeyRef指针
    if(_public_key == nil){
        NSData *certificateData =[GTMBase64 decodeString:RSA_KEY_BASE64];
        
        NSLog(@"%@",[certificateData description]);
        
        SecCertificateRef myCertificate =  SecCertificateCreateWithData(kCFAllocatorDefault, (CFDataRef)certificateData);
        SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
        SecTrustRef myTrust;
        OSStatus status = SecTrustCreateWithCertificates(myCertificate,myPolicy,&myTrust);
        SecTrustResultType trustResult;
        if (status == noErr) {
            status = SecTrustEvaluate(myTrust, &trustResult);
        }
        _public_key = SecTrustCopyPublicKey(myTrust);
        CFRelease(myCertificate);
        CFRelease(myPolicy);
        CFRelease(myTrust);
    }
    return _public_key;
}
*/

/*
+ (NSData*) rsaEncryptString:(NSString*) string
{
    SecKeyRef key = [HashUtility getPublicKey];
    size_t cipherBufferSize = SecKeyGetBlockSize(key);
    uint8_t *cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
    
    NSData *stringBytes = [string dataUsingEncoding:NSUTF8StringEncoding];
    size_t blockSize = cipherBufferSize - 11;
    size_t blockCount = (size_t)ceil([stringBytes length] / (double)blockSize);
    NSMutableData *encryptedData = [[[NSMutableData alloc] init] autorelease];
    
    for (int i=0; i<blockCount; i++) 
    {
        int bufferSize = MIN(blockSize,[stringBytes length] - i * blockSize);
        NSData *buffer = [stringBytes subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
        OSStatus status = SecKeyEncrypt(key, kSecPaddingPKCS1, (const uint8_t *)[buffer bytes],
                                        [buffer length], cipherBuffer, &cipherBufferSize);
        if (status == noErr)
        {
            NSData *encryptedBytes = [[NSData alloc] initWithBytes:(const void *)cipherBuffer length:cipherBufferSize];
            [encryptedData appendData:encryptedBytes];
            [encryptedBytes release];
        }
        else
        {
            if (cipherBuffer) free(cipherBuffer);
            return nil;
        }
    }
    if (cipherBuffer) free(cipherBuffer);
    //  NSLog(@"Encrypted text (%d bytes): %@", [encryptedData length], [encryptedData description]);
    NSLog(@"Encrypted text base64: %@", [encryptedData base64EncodedString]);
    return encryptedData;
}//
*/
@end